package xyf.p01.array_nodelist;

import commons.ListNode;

/**
 * ClassName: P0019_RemoveN
 * Description:
 * Author: xyf
 * Date: 9/7/22 7:44 PM
 * Version: 1.0
 **/
public class P0019_RemoveN
{
    public ListNode removeNthFromEnd(ListNode head, int n)
    {
        ListNode tmp = head;
        int size = 0;
        while (tmp != null)
        {
            tmp = tmp.next;
            size++;
        }

        int number = size - n;

        if (number == 0)
        {
            return head.next;
        }

        tmp = head;
        while (number > 0)
        {
            tmp = tmp.next;
            number--;
        }


        tmp.next = tmp.next.next;
        return head;
    }
}
